﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

namespace shop.Utils
{
    public class DB
    {

        public static SqlConnection createSqlConnection()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString);
            return conn;
        }

        public static void connectionOpen(SqlConnection conn)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
            catch
            {
                conn.Close();
            }
        }
        public static void connectionClose(SqlConnection conn)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            catch
            {
                conn.Close();
            }
        }


        public static Boolean executeSQL(string sql)
        {
            SqlConnection conn = null;
            try
            {
                conn = createSqlConnection();
                connectionOpen(conn);
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                connectionClose(conn);
                return true;
            }
            catch
            {
                connectionClose(conn);
                return false;
            }
        }


        public static DataSet getDataSet(string sql, string tableName)
        {
            SqlConnection conn = createSqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, tableName);
            return ds;
        }

        public static DataSet getDataSet(string sql)
        {
            SqlConnection conn = createSqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
    }


}
